﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
using System.Windows.Forms;

namespace BPP
{
    public partial class _programm
    {

        public class Metabolites
        {
            public string Abbreviation
            {
                get;
                set;
            }
            public string Name
            {
                get;
                set;
            }
            public string Formula_neutral
            {
                get;
                set;
            }
            public string Formula_charged
            {
                get;
                set;
            }
            public string Charge
            {
                get;
                set;
            }
            public string Compartment
            {
                get;
                set;
            }
            public string KEGG_ID
            {
                get;
                set;
            }
            public string PubChem_ID
            {
                get;
                set;
            }
            public string ChEBI_ID
            {
                get;
                set;
            }
            public string InChI
            {
                get;
                set;
            }
            public string SMILES
            {
                get;
                set;
            }
            public string Link
            {
                get;
                set;
            }


            public Metabolites(string[] arr)
            {
                Abbreviation = arr[0];
                Name = arr[1];
                Formula_neutral = arr[2];
                Formula_charged = arr[3];
                Charge = arr[4];
                Compartment = arr[5];
                KEGG_ID = arr[6];
                PubChem_ID = arr[7];
                ChEBI_ID = arr[8];
                InChI = arr[9];
                SMILES = arr[10];
                if(arr.Length>11)
                    Link = arr[11];
            }
            public Metabolites()
            {
                
            }

            //todo v shared funcitions
            public bool FillCompoundDataToGridView(string compound)
            {


                List<string> list = new List<string>();

                MySqlConnection connection = new MySqlConnection(shared_functions.pathToDataBase);
                MySqlCommand command = connection.CreateCommand();
                MySqlDataReader reader;

                command.CommandText = "select * from  compound_information where keggid ='" + compound + "'";

                bool readed = false;

                try
                {
                    connection.Open();

                    reader = command.ExecuteReader();

                    while (reader.Read())
                    {

                        this.Link = reader.GetValue(1).ToString();
                        this.Name = reader.GetValue(2).ToString();
                        this.Formula_charged = reader.GetValue(3).ToString();
                        this.Formula_neutral = reader.GetValue(4).ToString();
                        this.Charge = reader.GetValue(5).ToString();
                        this.Compartment = reader.GetValue(6).ToString();
                        this.KEGG_ID = reader.GetValue(7).ToString();
                        this.ChEBI_ID = reader.GetValue(8).ToString();
                        this.PubChem_ID = reader.GetValue(9).ToString();
                        this.InChI = reader.GetValue(10).ToString();
                        this.SMILES = reader.GetValue(11).ToString();
                        this.Abbreviation = reader.GetValue(12).ToString();


                        readed = true;


                        ///list.Add(reader.GetValue(2).ToString());
                    }

                    connection.Close();
                }
                catch (MySqlException ex)
                {
                    MessageBox.Show(ex.ToString());
                }

                return readed;

            }

            public void SaveToDb()
            {
                MySqlConnection connection = new MySqlConnection(_programm.shared_functions.pathToDataBase);
                MySqlCommand command = connection.CreateCommand();

                MySqlDataReader reader;
                bool result = false;
                command.CommandText = "select * from compound_information where keggid ='" + this.KEGG_ID + "'";
                try
                {
                    connection.Open();
                    reader = command.ExecuteReader();
                    if (reader.HasRows)
                    {
                        result = true;
                    }

                    connection.Close();
                }
                catch (MySqlException ex)
                {
                    MessageBox.Show(ex.ToString());
                }

                if (result)
                {
                    try
                    {
                        connection.Open();

                        command.CommandText = "update compound_information set " +
                              "link = '" + this.Link.TrimEnd().TrimStart() +
                              "', name = '" + this.Name.TrimEnd().TrimStart() +
                              "', formula_charged = '" + this.Formula_charged.TrimEnd().TrimStart() +
                              "', formula_neutral = '" + this.Formula_neutral.TrimEnd().TrimStart() +
                              "', charge = '" + this.Charge.TrimEnd().TrimStart() +
                              "', compartment = '" + this.Compartment.TrimEnd().TrimStart() +
                              "', keggid = '" + this.KEGG_ID.TrimEnd().TrimStart() +
                              "', chebiid = '" + this.ChEBI_ID.TrimEnd().TrimStart() +
                              "', pubchemid = '" + this.PubChem_ID.TrimEnd().TrimStart() +
                               "', inchi = '" + this.InChI.TrimEnd().TrimStart() +
                              "', smiles = '" + this.SMILES.TrimEnd().TrimStart() +
                              "', abbreviation = '" + this.Abbreviation.TrimEnd().TrimStart() + "' where keggid ='" + this.KEGG_ID + "'";

                        command.ExecuteScalar();

                        connection.Close();
                    }
                    catch (MySqlException ex)
                    {
                        MessageBox.Show(ex.ToString());
                    }
                }
                else
                {
                    //insert
                    try
                    {
                        connection.Open();

                        command.CommandText = "insert into  compound_information (link, name, formula_charged, formula_neutral,charge,compartment, " +
                        " keggid,chebiid, pubchemid,inchi,smiles,abbreviation) values ('"
                            + this.Link.TrimEnd().TrimStart() + "','"
                            + this.Name.TrimEnd().TrimStart() + "','"
                            + this.Formula_charged.TrimEnd().TrimStart() + "','"
                            + this.Formula_neutral.TrimEnd().TrimStart() + "','"
                            + this.Charge.TrimEnd().TrimStart() + "','"
                            + this.Compartment.TrimEnd().TrimStart() + "','"
                            + this.KEGG_ID.TrimEnd().TrimStart() + "','"
                            + this.ChEBI_ID.TrimEnd().TrimStart() + "','"
                            + this.PubChem_ID.TrimEnd().TrimStart() + "','"
                            + this.InChI.TrimEnd().TrimStart() + "','"
                            + this.SMILES.TrimEnd().TrimStart() + "','"
                            + this.Abbreviation.TrimEnd().TrimStart() + "')";

                        command.ExecuteScalar();

                        connection.Close();
                    }
                    catch (MySqlException ex)
                    {
                        MessageBox.Show(ex.ToString());
                    }
                }


            }

        }
    }
}
